package com.jihox.abookpro.util;
import com.jihox.abookpro.R;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "abookPro.db";
	private static final int DATABASE_VERSION = R.integer.database_version+1;

	public DBHelper(Context context) {
		//CursorFactory设置为null,使用默认值
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	//数据库第一次被创建时onCreate会被调用
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS thumbnails" +
				"(_data TEXT PRIMARY KEY, date_modified INTEGER,bucket_display_name TEXT,orientation INTEGER)");
		db.execSQL("CREATE TABLE IF NOT EXISTS orders" +
				"(OrderId TEXT PRIMARY KEY,Worksn TEXT,TemplateName TEXT, BookProject TEXT,ZipFilePath TEXT,ZipFileCrc TEXT,ZipFileSize INTEGER,FileUrl TEXT,OrderStatus INTEGER,OrderDeliveryCycle INTEGER,Thumbnail BLOB)");
		db.execSQL("CREATE TABLE IF NOT EXISTS bookproducts" + 
				"(ProductId TEXT PRIMARY KEY,Content TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS templates" + 
				"(TemplateId TEXT PRIMARY KEY,ProductId TEXT,TemplateVersion TEXT,Content TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS background" + 
				"(Name TEXT PRIMARY KEY,Picture BLOB)");
		db.execSQL("CREATE TABLE IF NOT EXISTS material" + 
				"(Name TEXT PRIMARY KEY,Picture BLOB)");
	}

	//如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}
}
